<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <link rel="canonical" href="https://caniphp.com/" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta
      name="og:title"
      content="Can I PHP? - caniuse.com but for PHP features"
    />
    <meta name="og:description" content="caniuse.com but for PHP features" />
    <meta
      name="og:image"
      content="https://can-i-use.com/images/can-i-php-screen.png"
    />
    <title>Can I PHP? - caniuse.com but for PHP features</title>
    <meta
      name="description"
      content="A list of PHP features and the versions of PHP that they are present in. Like caniuse.com but for PHP!"
    />
    <link href="/dist/style.css" rel="stylesheet" />
  </head>
  <body>
    <div
      id="app"
      x-data="caniphp"
      class="text-center pt-6 pb-24 px-6 md:w-2/3 max-w-4xl mx-auto"
    >
      <header class="mb-12 relative">
        <div class="mb-14 text-sm">
          <a
            class="underline hover:no-underline"
            href="https://github.com/rosswintle/can-i-php/blob/main/README.md"
            >README.md</a
          >
          &mdash;
          <a
            class="underline hover:no-underline"
            href="https://github.com/rosswintle/can-i-php"
            >GitHub</a
          >
          &mdash; By
          <a
            class="underline hover:no-underline"
            href="https://rw.omg.lol/"
            >Ross Wintle</a
          >
          &mdash;
          <a
            class="underline hover:no-underline"
            href="https://ko-fi.com/magicroundabout"
            >Buy me coffee</a
          >
          OR
          <a class="underline hover:no-underline" href="https://turbo-admin.com"
            >Check out my WordPress Browser Extension</a
          >
        </div>
        <div class="mb-4 text-2xl">
          <strong>Unofficial</strong> and <em>incomplete</em>
        </div>
        <h1 class="text-6xl tracking-wider font-bold mb-3">Can I PHP?</h1>
        <p class="text-center mx-auto lg:w-2/3">
          PHP feature support by version - like caniuse.com but for PHP.<br />
          This is a list of language features and functions added or removed in
          recent PHP versions. It is not an exhaustive reference.
        </p>
        <div class="w-auto px-6 py-4 bg-red-500 text-white font-bold absolute left-auto right-5 top-8 rotate-6">
            Now with added PHP 8.4!
        </div>
      </header>
      <main>
        <form action="" @submit.prevent="">
          <input
            x-model="searchString"
            type="search"
            name="s"
            placeholder="Search for a PHP feature"
            class="w-full md:w-2/3 mb-6 md:mb-8 text-xl md:text-3xl py-4 px-6 border-2 rounded-md"
          />
        </form>
        <p class="mb-2">
            Or view what's changed in a PHP version:
        </p>
        <ul class="flex flex-wrap w-full justify-center mb-12">
            <li>
                <button @click="versionFilter=''" class="leading-none mb-4 px-4 py-2 rounded-md transition" :class="versionFilter === '' ? 'bg-blue-300' : 'bg-gray-200'">All</button>
            </li>
            <template x-for="version in versions">
                <li>
                    <button x-text="version.version" @click="versionFilter=version.version" class="leading-none mb-4 px-4 py-2 rounded-md transition ml-4" :class="versionFilter === version.version ? 'bg-blue-300' : 'bg-gray-200'"></button>
                </li>
            </template>
        </ul>

        <div
          class="text-left prose lg:prose-xl"
          x-show="filteredFeatures().length === 0 && !loading"
          style="display: none"
        >
          <h3>Nothing matches!</h3>
          <p>It could be that:</p>
          <ul>
            <li>
              We know about the thing, but your search didn't find it - try
              again! If you think search didn't work for a given feature, please
              raise an issue or PR on
              <a href="https://github.com/rosswintle/can-i-php">
                the GitHub repo</a
              >
              to improve the keywords.
            </li>
            <li>
              The feature was not added in, deprecated from or removed from a
              recent PHP version. caniphp.com is not a list of ALL PHP functions
              and features, just those added or removed in recent versions.
            </li>
            <li>
              We don't know about the thing yet. The information here is being
              manually curated, and is not yet complete. So please bear with us
              as we add more data, or contribute what you were looking for on
              <a href="https://github.com/rosswintle/can-i-php">GitHub.</a>
            </li>
          </ul>
          <p>
            If you think something is missing then feel free to open a pull
            request on
            <a href="https://github.com/rosswintle/can-i-php"
              >the GitHub repo</a
            >
            to add it.
          </p>
        </div>

        <div class="text-left">
          <template x-for="feature in filteredFeatures()">
            <!-- Feature panel -->
            <article class="w-full mb-6">
              <!-- Feature info -->
              <div class="w-full border border-b-none py-2 px-4">
                <h2 x-text="feature.name" class="text-lg mb-4 font-bold"></h2>
                <div class="flex">
                  <!-- Feature top panel, left column -->
                  <div class="w-3/4 pr-4">
                    <p
                      x-html="formatAsHtml(feature.description)"
                      class="mb-6"
                    ></p>
                    <h3 class="font-bold text-l mb-2">Resources</h3>
                    <ul
                      x-show="feature?.resources?.length > 0"
                      class="list-disc list-inside text-sm mb-4"
                    >
                      <template x-for="resource in feature.resources">
                        <li>
                          <a
                            x-text="resource.name"
                            class="underline hover:no-underline"
                            :href="resource.url"
                          ></a>
                        </li>
                      </template>
                    </ul>
                  </div>
                  <div class="w-1/4 pl-4">
                    <p x-show="feature.added && feature.added !== '0.0'">
                      Added: PHP v<span x-text="feature.added"></span>
                    </p>
                    <p x-show="feature.deprecated">
                      Deprecated: PHP v<span x-text="feature.deprecated"></span>
                    </p>
                    <p x-show="feature.removed">
                      Removed: PHP v<span x-text="feature.removed"></span>
                    </p>
                  </div>
                </div>
              </div>
              <!-- Feature versions - colored boxes -->
              <div class="flex w-full overflow-auto">
                <template x-for="version in versions">
                  <div
                    class="flex-1 px-3 py-2 text-center"
                    :class="featureSupportedClass(feature, version.version)"
                  >
                    <span x-text="version.version"></span>
                  </div>
                </template>
              </div>
            </article>
          </template>
        </div>
      </main>
    </div>
    <!-- Fathom - beautiful, simple website analytics -->
    <script src="https://cdn.usefathom.com/script.js" data-site="IULNCQBS" defer></script>    <!-- / Fathom -->

    <script src="versions.js"></script>
    <script src="//unpkg.com/alpinejs" defer></script>

    <script>
      document.addEventListener("alpine:init", () => {

        document.querySelector("input[name=s]").focus();

        const url = new URL(window.location.href);

        const searchParam = url.searchParams.get("s");

        const versionFilter = url.searchParams.get("version");

        Alpine.data("caniphp", () => {
          return {
            // searchString: window.location.pathname.slice(1) ?? '',
            searchString: searchParam ?? "",

            features: [],

            loading: true,

            versions: versions,

            versionFilter: versionFilter ?? "",

            async init() {
              const featuresData = await fetch("/features.json");
              this.features = await featuresData.json();
              this.loading = false;

              this.$watch("searchString", (value) => {
                const url = new URL(window.location.href);
                if (value.length > 0) {
                    url.searchParams.set("s", value);
                } else {
                    url.searchParams.delete("s");
                }
                history.replaceState(null, "", url);
              });

              this.$watch("versionFilter", (value) => {
                const url = new URL(window.location.href);
                if (value.length > 0) {
                    url.searchParams.set("version", value);
                } else {
                    url.searchParams.delete("version");
                }
                history.replaceState(null, "", url);
              });
            },

            versionCompare(v1, v2) {
              if (v1 === v2) {
                return 0;
              }

              const v1parts = v1.split(".");
              const v2parts = v2.split(".");

              if (v1parts[0] > v2parts[0]) {
                return 1;
              }

              if (v1parts[0] < v2parts[0]) {
                return -1;
              }

              return v1parts[1] > v2parts[1] ? 1 : -1;
            },

            featureSupportedIn(feature, version) {
              // Can't tell if feature is added
              if (feature.added === null) {
                return false;
              }

              // Feature is not yet added in this version
              if (this.versionCompare(feature.added, version) === 1) {
                return false;
              }

              // Feature is added, removal is not specified
              if (feature.removed === null) {
                return true;
              }

              // Feature is removed
              if (this.versionCompare(feature.removed, version) <= 0) {
                return false;
              }

              // Feature is added and not removed
              return true;
            },

            featureIsDeprecatedIn(feature, version) {
                return feature.deprecated !== null
                    && this.versionCompare(feature.deprecated, version) <= 0;
            },

            featureIsRemovedIn(feature, version) {
                return feature.removed !== null
                    && this.versionCompare(feature.removed, version) <= 0;
            },

            featureSupportedClass(feature, version) {
              // Feature is deprecated, but not removed
              if (
                this.featureIsDeprecatedIn(feature, version) &&
                !this.featureIsRemovedIn(feature, version)
              ) {
                return "bg-amber-200";
              }

              return this.featureSupportedIn(feature, version)
                ? "bg-green-300 font-bold"
                : "bg-red-300";
            },

            filteredFeatures() {
                return this.changesInVersion(this.versionFilter, this.featuresHaving(this.searchString));
            },

            featureMatches(feature, keyword) {
              keyword = keyword.toLowerCase();
              return (
                feature.name.toLowerCase().includes(keyword) ||
                feature.description.toLowerCase().includes(keyword) ||
                feature.keywords.some((featureKeyword) =>
                  featureKeyword.toLowerCase().includes(keyword)
                )
              );
            },

            featuresHaving(featureKeyword) {
              return this.features.filter((feature) =>
                this.featureMatches(feature, featureKeyword)
              );
            },

            changesInVersion(version, features) {
                return features.filter((feature) =>
                    ! version ||
                    feature.added === version ||
                    feature.deprecated === version ||
                    feature.removed === version
                );
            },

            /*
             * This does some basic formatting.
             *
             *  - backticks are converted to <code></code>
             */
            formatAsHtml(rawString) {
                // Code blocks denoted by backticks
                tempString = rawString.replace(/`([\s\S]*?)`/g, "<code>$1</code>");
                // Change double newlines to pararaphs
                tempString = tempString.replace(/(?:[\r\n|\r|\n]{2})/g, '</p><p class="mb-4">');
                // Change single newlines to line breaks
                tempString = tempString.replace(/(?:\r\n|\r|\n)/g, '<br>');
                // Wrap the outer with paragraphs
                return '<p class="mb-4">' + tempString + '</p>';
            },
          };
        });
      });
    </script>
  </body>
</html>
